package com.yanqu.road.dao.impl.activity.langyabang;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.langyabang.config.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaBangConfigDaoImpl extends TempDao {
    private static String getLangYaCaseConfigSql = "select * from `t_s_activity_langya_case` where `activityId`=?";
    public Map<Integer,LangYaCaseConfig> getLangYaCaseConfigMap(int activityId) {
        Map<Integer,LangYaCaseConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLangYaCaseConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaCaseConfig langYaCaseConfig = new LangYaCaseConfig();
                langYaCaseConfig.setActivityId(rs.getInt("activityId"));
                langYaCaseConfig.setId(rs.getInt("id"));
                String initNum = rs.getString("initNum");
                List<Integer> initNumList = StringUtils.stringToIntegerList(initNum, "\\|");
                langYaCaseConfig.setInitNum(initNumList);
                String loseNum1 = rs.getString("loseNum1");
                List<Integer> lose1List = StringUtils.stringToIntegerList(loseNum1, "\\|");
                langYaCaseConfig.setLoseNum1(lose1List);

                String loseNum2 = rs.getString("loseNum2");
                List<Integer> lose2List = StringUtils.stringToIntegerList(loseNum2, "\\|");
                langYaCaseConfig.setLoseNum2(lose2List);

                langYaCaseConfig.setReward(rs.getString("reward"));
                langYaCaseConfig.setStar(rs.getInt("star"));
                langYaCaseConfig.setInitEvent(rs.getInt("initEvent"));

                map.put(langYaCaseConfig.getId(),langYaCaseConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getLangYaCaseEventConfigSql = "select * from `t_s_activity_langya_case_event` where `activityId`=?";
    public Map<Integer,Map<Integer,LangYaCaseEventConfig>> getLangYaCaseEventConfigMap(int activityId) {
        Map<Integer,Map<Integer,LangYaCaseEventConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLangYaCaseEventConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaCaseEventConfig langYaCaseEventConfig = new LangYaCaseEventConfig();
                langYaCaseEventConfig.setActivityId(rs.getInt("activityId"));
                langYaCaseEventConfig.setId(rs.getInt("id"));
                langYaCaseEventConfig.setCaseId(rs.getInt("caseId"));
                langYaCaseEventConfig.setQuestion(rs.getString("question"));
                langYaCaseEventConfig.setChooseA(rs.getString("chooseA"));
                langYaCaseEventConfig.setJumpA(rs.getInt("jumpA"));

                String effectA = rs.getString("effectA");
                List<Integer> effectAList = StringUtils.stringToIntegerList(effectA, "\\|");
                langYaCaseEventConfig.setEffectA(effectAList);
                langYaCaseEventConfig.setChooseB(rs.getString("chooseB"));
                langYaCaseEventConfig.setJumpB(rs.getInt("jumpB"));

                String effectB = rs.getString("effectB");
                List<Integer> effectBList = StringUtils.stringToIntegerList(effectB, "\\|");
                langYaCaseEventConfig.setEffectB(effectBList);

                Map<Integer, LangYaCaseEventConfig> caseEventConfigMap = map.computeIfAbsent(langYaCaseEventConfig.getCaseId(), k -> new ConcurrentHashMap<Integer, LangYaCaseEventConfig>());
                caseEventConfigMap.put(langYaCaseEventConfig.getId(),langYaCaseEventConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getLangYaBangAreaConfigSql = "select * from `t_s_activity_langya_area` where `activityId`=?";
    public Map<Integer,Map<Integer,LangYaBangAreaConfig>> getLangYaBangAreaConfigMap(int activityId) {
        Map<Integer,Map<Integer,LangYaBangAreaConfig>> configMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLangYaBangAreaConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaBangAreaConfig langYaBangAreaConfig = new LangYaBangAreaConfig();
                langYaBangAreaConfig.setActivityId(rs.getInt("activityId"));
                langYaBangAreaConfig.setAreaLvId(rs.getInt("areaLvId"));
                langYaBangAreaConfig.setAreaType(rs.getInt("areaType"));
                langYaBangAreaConfig.setLv(rs.getInt("lv"));
                langYaBangAreaConfig.setCost(rs.getString("cost"));
                langYaBangAreaConfig.setAreaValue(rs.getString("areaValue"));
                langYaBangAreaConfig.setAreaValueList(StringUtils.stringToIntegerList(langYaBangAreaConfig.getAreaValue(),"\\|"));
                langYaBangAreaConfig.setUnlockPart(rs.getString("unlockPart"));
                langYaBangAreaConfig.setCardLv(rs.getInt("cardLv"));
                langYaBangAreaConfig.setUpLimit(rs.getString("upLimit"));
                langYaBangAreaConfig.setUpLimitList(StringUtils.stringToIntegerList(rs.getString("upLimit"),"\\|"));
                langYaBangAreaConfig.setStep(rs.getInt("step"));
                langYaBangAreaConfig.setReward(rs.getString("reward"));
                Map<Integer, LangYaBangAreaConfig> integerLangYaBangAreaConfigMap = configMap.computeIfAbsent(langYaBangAreaConfig.getAreaType(), k-> new ConcurrentHashMap<>());
                integerLangYaBangAreaConfigMap.put(langYaBangAreaConfig.getAreaLvId(),langYaBangAreaConfig);

            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    private static String getLangYaBangBuildingConfigSql = "select * from `t_s_activity_langya_station` where `activityId`=?";
    public Map<Integer,LangYaBangBuildingConfig> getLangYaBangBuildingConfigMap(int activityId) {
        Map<Integer,LangYaBangBuildingConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLangYaBangBuildingConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaBangBuildingConfig langYaBangBuildingConfig = new LangYaBangBuildingConfig();
                langYaBangBuildingConfig.setActivityId(rs.getInt("activityId"));
                langYaBangBuildingConfig.setStationLv(rs.getInt("stationLv"));
                langYaBangBuildingConfig.setEffect(rs.getInt("effect"));
                langYaBangBuildingConfig.setAddPowerPro(rs.getInt("addPowerPro"));
                langYaBangBuildingConfig.setAddPowerFix(rs.getString("addPowerFix"));
                langYaBangBuildingConfig.setEnergyCostChoose(rs.getInt("energyCostChoose"));
                map.put(langYaBangBuildingConfig.getStationLv(),langYaBangBuildingConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getLangYaBangPveConfigSql = "select * from `t_s_activity_langya_npc` where `activityId`=?";
    public Map<Integer,LangYaBangPveConfig> getLangYaBangPveConfigList(int activityId) {
        Map<Integer,LangYaBangPveConfig> configMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLangYaBangPveConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaBangPveConfig langYaBangPveConfig = new LangYaBangPveConfig();
                langYaBangPveConfig.setActivityId(rs.getInt("activityId"));
                langYaBangPveConfig.setNpcId(rs.getInt("npcId"));
                langYaBangPveConfig.setName(rs.getString("name"));
                langYaBangPveConfig.setDesc(rs.getString("desc"));
                langYaBangPveConfig.setBaseCoin(rs.getInt("baseCoin"));
                langYaBangPveConfig.setExCoin(rs.getInt("exCoin"));
                langYaBangPveConfig.setReward(rs.getString("reward"));
                langYaBangPveConfig.setQuality(rs.getInt("quality"));
                configMap.put(langYaBangPveConfig.getNpcId(),langYaBangPveConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    private static String getLangYaRollDiceConfigSql = "select * from `t_s_activity_langya_draw` where `activityId`=?";
    public Map<Integer,Map<Integer,LangYaRollDiceConfig>> getLangYaRollDiceConfigMap(int activityId) {
        Map<Integer,Map<Integer,LangYaRollDiceConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLangYaRollDiceConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaRollDiceConfig langYaRollDiceConfig = new LangYaRollDiceConfig();
                langYaRollDiceConfig.setActivityId(rs.getInt("activityId"));
                langYaRollDiceConfig.setId(rs.getInt("id"));
                langYaRollDiceConfig.setType(rs.getInt("type"));
                langYaRollDiceConfig.setWeight(rs.getInt("weight"));
                langYaRollDiceConfig.setEventParam(rs.getString("eventParam"));
                if (langYaRollDiceConfig.getType() != 8){
                    langYaRollDiceConfig.setEventParamList(StringUtils.stringToIntegerList(rs.getString("eventParam"),"\\|"));
                }else {
                    langYaRollDiceConfig.setEventParamList(new ArrayList<>());
                }

                langYaRollDiceConfig.setBaseProduce(rs.getString("baseProduce"));
                if (langYaRollDiceConfig.getType() == 9 || langYaRollDiceConfig.getType() == 10){
                    langYaRollDiceConfig.setBaseProduceList(new ArrayList<>());
                }else {
                    langYaRollDiceConfig.setBaseProduceList(StringUtils.stringToIntegerList(rs.getString("baseProduce"),"\\|"));
                }

                langYaRollDiceConfig.setReward(rs.getString("reward"));
                langYaRollDiceConfig.setProtectNum(rs.getInt("protectNum"));
                langYaRollDiceConfig.setUnlock(rs.getInt("unlock"));
                langYaRollDiceConfig.setAddType(rs.getInt("addType"));
                langYaRollDiceConfig.setQuality(rs.getInt("quality"));
                Map<Integer, LangYaRollDiceConfig> integerLangYaRollDiceConfigMap = map.computeIfAbsent(langYaRollDiceConfig.getType(), k-> new ConcurrentHashMap<>());
                integerLangYaRollDiceConfigMap.put(langYaRollDiceConfig.getId(),langYaRollDiceConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


}
